using AutoMapper;
using SmartLink.Application.IServices;
using SmartLink.Domain;
using SmartLink.Domain.Entity;
using SmartLink.Domain.ExpandEntity.Base;
using SmartLink.Domain.IServices;
using SmartLink.DTOS;
using SmartLink.DTOS.Permission;
using SmartLink.EnumLibrary;
using SmartLink.Infrastructure;
using SmartLink.Infrastructure.Cache;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartLink.Application.Services
{
    public class RoleAppServices : IRoleAppServices
    {
        private IRoleServices _roleServices;

        public RoleAppServices(IRoleServices roleServices)
        {
            _roleServices = roleServices;
        }

        public Results AddRole(RoleAddDTO model)
        {
            var role = Mapper.Map<PER_RoleGroup>(model);
            role.Create(CurrentUser.Current.UserId, CurrentUser.Current.OID, CurrentUser.Current.NickName);
            var opResult = _roleServices.AddRole(role);
            return InfrastructureAppService.Transmit(opResult);
        }

        public Results<RoleDTO> GetRole(int id)
        {
            var entity = _roleServices.GetRole(id);
            var model = Mapper.Map<RoleDTO>(entity);
            return InfrastructureAppService.Response(model);
        }

        public Results UpdateRole(RoleUpdateDTO model)
        {
            var role = Mapper.Map<PER_RoleGroup>(model);
            var opResult = _roleServices.UpdateRole(role);
            return InfrastructureAppService.Transmit(opResult);
        }

        public Results DeleteRole(int id)
        {
            var opResult = _roleServices.DeleteRole(id);
            return InfrastructureAppService.Transmit(opResult);
        }

        public Results<PageData<RoleListItemDTO>> GetPageList(RolePagePara pagePara)
        {
            var pagination = new Pagination(pagePara.PageIndex, pagePara.PageSize, pagePara.IsSearchTotal);
            var list = _roleServices.GetRolePaged(pagePara.Enabled, pagePara.KeyWord, pagination);
            var dto = Mapper.Map<IEnumerable<RoleListItemDTO>>(list);
            var pageData = new PageData<RoleListItemDTO> { CurrentData = dto, TotalCount = pagination.TotalCount };
            return InfrastructureAppService.Response(pageData);
        }

        public Results<IList<SelectItemDTO>> GetRoleSelectList(int? oid = null)
        {
            if (!oid.HasValue) oid = CurrentUser.Current.ROID;
            var roles = _roleServices.GetRoleListByOID(oid.Value);
            IList<SelectItemDTO> models = roles.Select(a => new SelectItemDTO { Value = a.ID.ToString(), Text = a.RoleName }).OrderBy(a => a.Value).ToList();
            return InfrastructureAppService.Response(models);
        }

        public Results AllotRolesToUser(AllotRoleToUserDTO allotRoleToUserDTO)
        {
            _roleServices.AllotRolesToUser(allotRoleToUserDTO.UserId, allotRoleToUserDTO.RoleIds.ToList());
            UserInfoCaches.RefreshUserRoleRelation(allotRoleToUserDTO.UserId, allotRoleToUserDTO.RoleIds.ToList());
            return InfrastructureAppService.Response();
        }
    }
}
